﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.ApplicationBlocks.Data;
using GotDotNet.ApplicationBlocks.Data;
using System.Data;
using System.Data.SqlClient;
using LibraryUtility.Web;


namespace LibraryUtility.Common
{
    /// <summary>
    /// ClassName:ExecTran
    /// Auther:Leo.wl
    /// Time:2010-04-14
    /// ExecTran 的摘要说明。
    /// </summary>
    public class ExecTran
    {
        /// <summary>
        /// connString is Conn DataBase.
        /// </summary>
        private static readonly string connString = ConfigHelper.GetConfigString("SqlServerConnectionString");


        /// <summary>
        /// Initializes a new instance of the <see cref="ExecTran"/> class.
        /// </summary>
        public ExecTran()
        {
            //
            // TODO: 开发CloseBook
            //
        }


        /// <summary>
        /// Trans the specified command text S.
        /// </summary>
        /// <param name="commandTextS">The command text S.</param>
        /// <returns></returns>
        public static int Tran(string[] commandTextS)
        {
            int rel = 0;

            SqlConnection con = new SqlConnection(connString);
            con.Open();
            SqlTransaction st = con.BeginTransaction();

            try
            {
                int ii = commandTextS.Length;
                for (int i = 0; i < ii; i++)
                {
                    SqlHelper.ExecuteNonQuery(st, CommandType.Text, commandTextS[i].ToString());
                }

                st.Commit();


                rel = 1;

            }
            catch (SqlException ee)
            {
                st.Rollback();
                throw ee;

            }
            finally
            {
                st.Dispose();
                con.Close();
                con.Dispose();
            }

            return rel;

        }


        /// <summary>
        /// Executes the SQLS.
        /// </summary>
        /// <param name="commandTextS">The command text S.</param>
        /// <returns></returns>
        public static bool ExecuteSQLS(string[] commandTextS)
        {
            bool returnValue = true;
            SqlConnection myConnection = new SqlConnection(connString);
            myConnection.Open();
            SqlTransaction myTrans = myConnection.BeginTransaction();
            SqlCommand myCommand = myConnection.CreateCommand();
            myCommand.Connection = myConnection;
            myCommand.Transaction = myTrans;
            try
            {
                for (int i = 0; i < commandTextS.Length; i++)
                {
                    myCommand.CommandText = commandTextS[i].ToString();
                    myCommand.ExecuteNonQuery();
                }
                myTrans.Commit();
            }
            catch
            {
                myTrans.Rollback();
                returnValue = false;
            }
            myTrans.Dispose();
            myCommand.Dispose();
            myCommand.Connection.Close();
            return returnValue;
        }


    }
}
